<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加新角色</title>
<link href="../css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://pic2.58.com/n/tuan/js/jquery-1.4.2.min.js"></script>
</head>
<body>
<script >
		var functions=",";
		var opers=",";

		$(function(){
		 	$(".menupri input:checkbox.selectOne").click(function (){
			 	var id= $(this).attr("value");
			 	var a = ","+id+",";
			 	if($(this).attr("checked")==false){
			 		$(this).prevAll(".selectAll").attr("checked",false);
			 		functions=functions.replace(eval("/"+a+"/g"),",");
			 	}
			 	else{
	
			 		if(functions.indexOf(a) < 0){
						functions=functions+id+",";
					}
					var f=true;
					$(this).prevAll(".selectAll").nextAll(":checkbox").each(function(){
	    				if($(this).attr("checked")==false)
	    					f=false;
	  				});
	  				$(this).prevAll(".selectAll").attr("checked",f);
			 	}	
			 });
			 
			 $(".menupri input:checkbox.selectAll").click(function (){
			 		var flag=$(this).attr("checked");
			 		$(this).nextAll(':checkbox').attr("checked",flag);
			 		$(this).nextAll(':checkbox').each(function(){
	    				var id= $(this).attr("value");
	    				var a = ","+id+",";
	    				if(flag && functions.indexOf(a) < 0)
							functions=functions+id+",";
						else if(flag==false){
							functions=functions.replace(eval("/"+a+"/g"),",");
						}
	  				});
			 });
			 
			 
			  $(".operlist input:checkbox.selectOne").click(function (){
			 	var id= $(this).attr("value");
			 	var a = ","+id+",";
			 	if($(this).attr("disabled")==false){
			 	if($(this).attr("checked")==false){
			 		$(this).prevAll(".selectAll").attr("checked",false);
			 		opers=opers.replace(eval("/"+a+"/g"),",");
			 	}
			 	else{
			 		if(opers.indexOf(a) < 0){
						opers=opers+id+",";
					}
					var f=true;
					$(this).prevAll(".selectAll").nextAll(":checkbox").each(function(){
	    				if($(this).attr("checked")==false)
	    					f=false;
	  				});
	  				$(this).prevAll(".selectAll").attr("checked",f);
			 	}
			  }
			 });
			 
			 
			 $(".operlist input:checkbox.selectAll").click(function (){
			 		var flag=$(this).attr("checked");
			 		$(this).nextAll(':checkbox').attr("checked",flag);
			 		$(this).nextAll(':checkbox').each(function(){
	    				var id= $(this).attr("value");
	    				var a = ","+id+",";
	    				if(flag && opers.indexOf(a) < 0)
							opers=opers+id+",";
						else if(flag==false){
							opers=opers.replace(eval("/"+a+"/g"),",");
						}
	  				});
			 });
			 
		 });
		
		 
		 
		 function checkAll(){
		 	  var flag=false;
			  var re = /^[\w|\u4E00-\u9FA5]+$/;
			  
			  var rolename = $("#rolename").val();
			  
			  if(rolename == null || rolename == ""){
			  	alert("请填写角色名称！");
			  }else if(!rolename.match(re)){
			  	alert("用户名仅可使用汉字、字母、数字或下划线。");
				$("#rolename").val("");
			  }else if(functions == null || functions == ""){
			  	alert("请添加权限!");
			  }
			  else{
			 	  flag=true;
			  }
			  return  flag;
		 }


		  $(function(){$("#toadd").click(function(){ 
		 		if(checkAll()){
    		 		jQuery.ajax({
    						url:"/rolemanager/toaddrole",
    						type:"post",
    						async:true,
    						data:{"rolename":$("#rolename").val(),"functions":functions,"opers":opers},
    						success:function(data){
    							if(data == "success"){
    								alert("添加成功！");
    							}else if(data == "has"){
									alert("角色已存在！");
								}else{
    								alert("添加失败！");
    							}
    						}
    				});
				}
			 })
		 })
		
</script>
<div class="lr_both">
<div class="right_conts">
  <div class="mg_tt">
    <div class="right_search">
      <div class="rs_title_bg">
        <h2 class="rs_title">角色添加</h2>
      </div>
	  
    </div>
	</br>
	</br>
	<form >
    <div class="table_div" align="center">
	  <input type="hidden" id="bcs" name="bcs"/>
	  <input type="hidden" id="isbusiness" name="isbusiness"/>
      <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
          <th align="center" colspan="2">角色基本信息</th>
		</tr>
		<tr>
           <td align="center" >角色名称</td>
		  <td align="center"  ><input type="text"  name="rolename" id="rolename"/></td>
        </tr>
	 </table>
		</br>
	 <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
           <th align="center" colspan="2">菜单权限</th>
		  </th>
		</tr>
		
		#foreach($key in $menuMap.keySet())
		<tr class="menupri">
			#set($cate1 = $key.split('-')[0])
			#set($cate2 = $key.split('-')[1])
			<td align="center" width ="20%"><span style="color:red">$cate1</span> </br> <span style="color:blue">$cate2</span></td>
			<td align="left"  class="menupri">
			 	<input type="checkbox" class="selectAll">&nbsp 全选 </input>&nbsp;&nbsp;
				#foreach($menu in $menuMap.get($key))
                    <input type="checkbox" class="selectOne" name="function_$menu.id" id="function_$menu.id" value="$menu.id">&nbsp $menu.name</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				#end
			 </td>		 
		</tr>
		#end
      </table>
	  </br> 
	  
	   <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
		<tr>
           <th align="center" colspan="2">数据操作细粒度权限</th>
		  </th>
		</tr>
		
		#foreach($key in $operMenuMap.keySet())
		 <tr class="operlist">
			 #set($cate1 = $key.split('-')[0])
		  	 #set($cate2 = $key.split('-')[1])
             <td align="center" width ="20%"><span style="color:red">$cate1</span> </br> <span style="color:blue">$cate2</span></td>
			 <td align="left" >
			 	<input type="checkbox" class="selectAll">&nbsp 全选 </input>&nbsp;&nbsp;
				#foreach($oper in $operMenuMap.get($key))
                    <input type="checkbox" class="selectOne" name="opers_$oper.id" id="opers_$oper.id" value="$oper.id">&nbsp $oper.name</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				#end
			 </td>
			 
		</tr>
		#end
      </table>
	  
	  <table class="sj_tl"  width="90%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
          <th align="center" colspan="2">
            <input type="button"  id="toadd" name="toadd"  value="保存"/>
			&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;	&nbsp;
			<input type="reset"    value="重置"/>
		  </th>
		</tr>
      </table>
    </div>
	</form>
  </div>

</div>
</div>
</body>
</html>
